<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;

use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;

use App\Admin\Models\distributionGroup;
use App\Admin\Models\Common;

use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;

class DistributionGroupController extends Controller
{
	use HasResourceActions;
	
    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('分销规则组')
            ->description('列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('分销规则组')
            ->description('详细')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('分销规则组')
            ->description('编辑')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('分销规则组')
            ->description('新增')
            ->body($this->form());
    }
    

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new DistributionGroup);
        $grid->dlgId('ID');
        $grid->dlgName('分销规则组名称');
        $grid->dlgMemo('分销规则组备注');
        $grid->createUser('上传人');
        $grid->createTime('上传时间');

		$grid->filter(function($filter){

		    // 去掉默认的id过滤器
		    $filter->disableIdFilter();

		});
		
		$grid->actions(function ($actions) {
		    $actions->disableDelete();
//		    $actions->disableEdit();
//		    $actions->disableView();
		});

		/*$grid->filter(function($filter){

		    // 去掉默认的id过滤器
		    $filter->disableIdFilter();
		    
		    $filter->column(1/2, function ($filter) {
		    	
		    	$filter->like('dpName', '返利名称');

			});
			

		});*/

		// 默认展开筛选框
		$grid->expandFilter();
		
		// 禁用行选择checkbox
		$grid->disableRowSelector();
		
		// 禁用导出
		$grid->disableExport();
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
    	$DistributionPrecept = new DistributionGroup();
        $show = new Show($DistributionPrecept->findOrFail($id));

        $show->dlgId('ID');
        $show->dlgName('分销规则组名称');
        $show->dlgMemo('分销规则组备注');
		$show->panel()
			->tools(function ($tools) {
//				$tools->disableEdit();
//				$tools->disableList();
				$tools->disableDelete();
		});;
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::Form(DistributionGroup::class, function (Form $form) {

			$form->hidden('dlgId');
			$form->text('dpName', '分销规则组名称');
			$form->text('dlgMemo', '分销规则组备注');
			$Common = new DistributionGroup();
    		$levelInfo = $Common->getDistributionLevel();
    		ksort($levelInfo);
    		$distributionPrecept = $Common->getDistributionPrecept();
    		foreach($levelInfo as $key=>$lev){
				$form->select('level_'.$key, $lev.'等级')->options($distributionPrecept);
			}
			
            $form->tools(function (Form\Tools $tools) {

			    // 去掉`列表`按钮
//			    $tools->disableList();
				
			    // 去掉`删除`按钮
			    $tools->disableDelete();
			
			    // 去掉`查看`按钮
//			    $tools->disableView();
			
			    // 添加一个按钮, 参数可以是字符串, 或者实现了Renderable或Htmlable接口的对象实例
//			    $tools->add('<a class="btn btn-sm btn-danger"><i class="fa fa-trash"></i>&nbsp;&nbsp;delete</a>');
			});
			
			$form->footer(function ($footer) {

//			    // 去掉`重置`按钮
//			    $footer->disableReset();
//			
//			    // 去掉`提交`按钮
//			    $footer->disableSubmit();
			
			    // 去掉`查看`checkbox
			    $footer->disableViewCheck();
			
			    // 去掉`继续编辑`checkbox
			    $footer->disableEditingCheck();
			
			    // 去掉`继续创建`checkbox
//			    $footer->disableCreatingCheck();
			
			});
        });
    }
    
}
